// pages/user/recovery.js
var utils = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        gender: [
            { name: '男', value: 'male' },
            { name: '女', value: 'female' }
        ],
        eduLevel: [
            { name: '初中及以下', value: '0' },
            { name: '高中/中专/大专', value: '1' },
            { name: '大学本科', value: '2'},
            { name: '研究生', value: '3'},
            { name: '博士及以上', value: '4'}
        ],
        date: "点击选择",// 2000-01-01
        countries: ["北京市","天津市","河北省","山西省","内蒙古自治区","辽宁省","吉林省","黑龙江省","上海市","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","广西壮族自治区","海南省","重庆市","四川省","贵州省","云南省","西藏自治区","陕西省","甘肃省","青海省","宁夏回族自治区","新疆维吾尔自治区","香港","澳门","台湾","海外"],
        countryIndex: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({

        })
    },

    bindDateChange: function (e) {
        this.birthday = e.detail.value
        this.setData({
            date: e.detail.value
        })
    },

    bindPosChange: function (e) {
        this.pos = this.data.countries[e.detail.value];
        this.setData({
            countryIndex: e.detail.value
        })
    },

    genderChange: function (e) {
        // console.log(e)
        var radioItems = this.data.gender;
        for (var i = 0, len = radioItems.length; i < len; ++i) {
            radioItems[i].checked = radioItems[i].value == e.detail.value;
        }
        this.gender = e.detail.value;
        this.setData({
            gender: radioItems
        });
    },

    eduChange: function (e) {
        var radioItems = this.data.eduLevel;
        for (var i = 0, len = radioItems.length; i < len; ++i) {
            radioItems[i].checked = radioItems[i].value == e.detail.value;
        }
        this.eduLevel = e.detail.value;
        this.setData({
            eduLevel: radioItems
        });
    },

    recover: function () {
        if (this.gender===undefined) this.warn("请填写性别！")
        else if (this.eduLevel === undefined) this.warn("请填写受教育程度！")
        else if (this.birthday === undefined) this.warn("请填写出生日期！")
        else if (this.pos === undefined) this.warn("请填写居住地！")
        else{
            const that = this;
            utils.httpRequest({
                method: "POST",
                url: 'https://www.water-crystal.org/Home/WechatApp/userRecovery?r=' + Math.random(),
                data: {
                    gender: that.gender,
                    edu: that.eduLevel,
                    birthday: that.birthday,
                    pos: that.pos
                },
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                    if (res.statusCode==200 && res.data.error===undefined){
                        wx.showModal({
                            title: '数据找回成功！',
                            content: "点击确定返回我的实验列表",
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    wx.redirectTo({
                                        url: '/pages/exp/exp'
                                    })
                                }
                            }
                        })
                    }else{
                        wx.showModal({
                            title: '数据找回失败！',
                            content: "请将您的这些信息从公众号后台发送给我们，人工恢复数据。",
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    wx.redirectTo({
                                        url: '/pages/exp/exp'
                                    })
                                }
                            }
                        })
                    }
                },
                fail: function(res){
                    console.log(res)
                }
            })
        }
    },

    warn: function(msg){
        wx.showModal({
            title: '信息不完整',
            content: msg,
            showCancel: false
        })
    }

})